package ForkJoinDemo;

import org.junit.*;

import java.time.Instant;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
import java.util.stream.LongStream;

public class TestForkJoin {

    @Test
    public void test(){
        Instant start = Instant.now();
        RecursiveTask<Long> forkJoinCalculate = new ForkJoinCalculate(0, 10000000000L);
        ForkJoinPool forkJoinPool=new ForkJoinPool();
        Long sumResult = forkJoinPool.invoke(forkJoinCalculate);
        Instant end = Instant.now();
        System.out.println(sumResult+", 耗时为："+(end.getNano()-start.getNano()));

    }

    @Test
    public void test1(){
        long sum=0;
        Instant start=Instant.now();
        for(long i=1;i<=10000000000L;i++){
            sum+=i;
        }
        Instant end=Instant.now();
        System.out.println(sum+", 耗时为："+(end.getNano()-start.getNano()));
    }

    @Test
    public void test2(){
        Instant start=Instant.now();
        long sum = LongStream.range(0, 100000000L).parallel().reduce(0, (x, y) -> x + y);
        Instant end=Instant.now();
        System.out.println(sum+", 耗时为："+(end.getNano()-start.getNano()));
    }
}
